|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
Now that weve copied the capture region from the screen to out bitmap in memory, we can place it into the clipboard. We do that by opening and emptying the clipboard, using SetClipboardData() to place the data in the clipboard and closing the clipboard again:
void CCopierView::OnRButtonUp(UINT nFlags, CPoint point)
{
if(CaptureFlag && DrawFlag){
DrawFlag = false;
.
.
.
MemoryDC.CreateCompatibleDC(&ScreenDC);
bitmap.CreateCompatibleBitmap(&ScreenDC, Width, Height);
MemoryDC.SelectObject(&bitmap);
MemoryDC.BitBlt(0, 0, Width, Height, &ScreenDC,
LeftUpper.x, LeftUpper.y, SRCCOPY);
OpenClipboard(); ⇐
EmptyClipboard(); ⇐
SetClipboardData(CF_BITMAP, HBITMAP(bitmap)); ⇐
CloseClipboard(); ⇐
.
.
.
}
CView::OnRButtonUp(nFlags, point);
}
This enables the user to use our program to copy sections of the screen into the clipboard. The last step in this program is to display the bitmap in the view. Displaying a Bitmap in a ViewTo copy the data in our bitmap to the programs view, we simply use BitBlt() one more time, this time to the client device context weve already set upScreenDC:
void CCopierView::OnRButtonUp(UINT nFlags, CPoint point)
{
if(CaptureFlag && DrawFlag){
DrawFlag = false;
.
.
.
MemoryDC.CreateCompatibleDC(&ScreenDC);
bitmap.CreateCompatibleBitmap(&ScreenDC, Width, Height);
MemoryDC.SelectObject(&bitmap);
MemoryDC.BitBlt(0, 0, Width, Height, &ScreenDC,
LeftUpper.x,
LeftUpper.y, SRCCOPY);
OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_BITMAP, HBITMAP(bitmap));
CloseClipboard();
CClientDC dc(this); ⇐
dc.BitBlt(0, 0, Width, Height, &ScreenDC, LeftUpper.x, LeftUpper.y,
SRCCOPY); ⇐
}
CView::OnRButtonUp(nFlags, point);
}
You are now ready to run the program. Press and hold the left mouse button in the program, then move to the beginning of the capture area. Drag the right mouse button over the capture area, drawing a rectangle, as shown in Figure 6.1. When you release the right mouse button, the captured region is copied to the clipboard and to our programs view, as shown in Figure 6.2. Now were able to perform screen capture. The code for this example, CopierView.h and CopierView.cpp, appear in Listing 6.1. Listing 6.1 CopierView.h and CopierView.cpp
// CopierView.h : interface of the CCopierView class
//
/////////////////////////////////////////////////////////////////////////////
#if
!defined(AFX_COPIERVIEW_H__44745D0F_A238_11D1_887F_
D42B07C10710__INCLUDED_)
#define AFX_COPIERVIEW_H__44745D0F_A238_11D1_887F_
D42B07C10710__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CCopierView : public CView
{
protected: // create from serialization only
CCopierView();
DECLARE_DYNCREATE(CCopierView)
bool CaptureFlag, DrawFlag;
CPoint AnchorPoint, PreviousPoint;
// Attributes
public:
CCopierDoc* GetDocument();
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCopierView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CCopierView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CCopierView)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in CopierView.cpp
inline CCopierDoc* CCopierView::GetDocument()
{ return (CCopierDoc*)m_pDocument; }
#endif
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional
declarations immediately before the previous line.
#endif // !defined(AFX_COPIERVIEW_H__44745D0F_A238_11D1_887F_
D42B07C10710__INCLUDED_)
// CopierView.cpp : implementation of the CCopierView class
//
#include "stdafx.h"
#include "Copier.h"
#include "CopierDoc.h"
#include "CopierView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCopierView
IMPLEMENT_DYNCREATE(CCopierView, CView)
BEGIN_MESSAGE_MAP(CCopierView, CView)
//{{AFX_MSG_MAP(CCopierView)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCopierView construction/destruction
CCopierView::CCopierView()
{
// TODO: add construction code here
CaptureFlag = false;
DrawFlag = false;
}
CCopierView::~CCopierView()
{
}
BOOL CCopierView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCopierView drawing
void CCopierView::OnDraw(CDC* pDC)
{
CCopierDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CCopierView printing
BOOL CCopierView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCopierView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCopierView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CCopierView diagnostics
#ifdef _DEBUG
void CCopierView::AssertValid() const
{
CView::AssertValid();
}
void CCopierView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCopierDoc* CCopierView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCopierDoc)));
return (CCopierDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCopierView message handlers
void CCopierView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetCapture();
CaptureFlag = true;
DrawFlag = false;
CView::OnLButtonDown(nFlags, point);
}
void CCopierView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
ReleaseCapture();
CaptureFlag = false;
DrawFlag = false;
CView::OnLButtonUp(nFlags, point);
}
void CCopierView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(CaptureFlag && DrawFlag){
CDC ScreenDC;
CPoint CurrentPoint = GetMessagePos();
ScreenDC.CreateDC("DISPLAY", NULL, NULL, NULL);
ScreenDC.SelectStockObject(NULL_BRUSH);
ScreenDC.SetROP2(R2_NOT);
ScreenDC.Rectangle(CRect(PreviousPoint, AnchorPoint));
ScreenDC.Rectangle(CRect(AnchorPoint, CurrentPoint));
PreviousPoint = CurrentPoint;
}
CView::OnMouseMove(nFlags, point);
}
void CCopierView::OnRButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(CaptureFlag && DrawFlag){
DrawFlag = false;
int Width, Height;
CPoint CurrentPoint = GetMessagePos();
CDC ScreenDC;
ScreenDC.CreateDC("DISPLAY", NULL, NULL, NULL);
ScreenDC.SelectStockObject(NULL_BRUSH);
ScreenDC.SetROP2(R2_NOT);
ScreenDC.Rectangle(CRect(AnchorPoint, PreviousPoint));
CPoint LeftUpper(min(AnchorPoint.x, CurrentPoint.x),
min(AnchorPoint.y, CurrentPoint.y));
Height = abs(AnchorPoint.y - CurrentPoint.y);
Width = abs(AnchorPoint.x - CurrentPoint.x);
CDC MemoryDC;
CBitmap bitmap;
MemoryDC.CreateCompatibleDC(&ScreenDC);
bitmap.CreateCompatibleBitmap(&ScreenDC, Width, Height);
MemoryDC.SelectObject(&bitmap);
MemoryDC.BitBlt(0, 0, Width, Height, &ScreenDC,
LeftUpper.x, LeftUpper.y, SRCCOPY);
OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_BITMAP, HBITMAP(bitmap));
CloseClipboard();
CClientDC dc(this);
dc.BitBlt(0, 0, Width, Height, &ScreenDC, LeftUpper.x,
LeftUpper.y, SRCCOPY);
}
CView::OnRButtonUp(nFlags, point);
}
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |